package com.ease.business.importing.importer;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ease.business.importing.vo.AttributeTemplate;
import com.ease.business.importing.vo.DataGridSource;
import com.ease.business.importing.vo.RowValidationParam;
import com.ease.common.util.Detect;

/**
 * @author nathanleewei
 * 
 */
public abstract class AttributeImporter extends Detect {

	private static final transient Log log = LogFactory.getLog(AttributeImporter.class);

	protected DataGridSource<?> dataGridSource;

	protected int taskIndex;

	protected int rowIndex;

	protected int columnIndex;

	protected AttributeTemplate attributeTemplate;

	protected Object object;

	protected Object value;

	protected String validationLabel;

	protected RowValidationParam rowValidationParam;

	public abstract void validate();

	public abstract void validateUniqueness();

	public abstract Object getCellObject(int rowIndex, int columnIndex);

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void setProperty() {
		String attributeCode = attributeTemplate.getCode();
		try {
			if (object instanceof Map) {
				((Map) object).put(attributeCode, value);
			} else {
				BeanUtils.setProperty(object, attributeCode, value);
			}
		} catch (Exception e) {
			log.error("rowIndex=" + rowIndex + ",columnIndex=" + columnIndex + ", value=" + value, e);
		}
	}

	// public HSSFCell getCell(int rowIndex, int columnIndex) {
	// // return dataGridSource.getCell(rowIndex, columnIndex);
	// HSSFRow rawRow = dataGridSource.getRawRow(rowIndex);
	// if (null != rawRow) {
	// return rawRow.getCell(columnIndex);
	// }
	// return null;
	// }

	// public Object getCellObject(int rowIndex, int columnIndex) {
	// HSSFCell cell = getCell(rowIndex, columnIndex);
	// if (null != cell) {
	// return WorkbookUtil.getCellObject(cell);
	// }
	// return null;
	// }

	public void populate() {
		value = asString(getCellObject(rowIndex, columnIndex), "");
		// if("OBDSPLITRATIO".equals(attributeTemplate.getAttributeCode())) {
		// double v = Double.parseDouble(String.valueOf(value));
		// value = "1:"+(int)1/v;
		// }
		if (true == attributeTemplate.isNotNull()) {
			rowValidationParam.validate(notEmpty(String.valueOf(value)), "", columnIndex, attributeTemplate.getName() + "不能为空");
		}
		this.validate();
		// this.validateUniqueness();
		this.setProperty();
	}

	public static int getLength(String string) {
		String newStr = null;
		try {
			newStr = new String(string.getBytes("GBK"), "ISO8859_1");
			return newStr.length();
		} catch (UnsupportedEncodingException e) {
			log.error(string, e);
		}
		return string.length();
	}

	/**
	 * @return the taskIndex
	 */
	public int getTaskIndex() {
		return taskIndex;
	}

	/**
	 * @param taskIndex
	 *            the taskIndex to set
	 */
	public void setTaskIndex(int taskIndex) {
		this.taskIndex = taskIndex;
	}

	/**
	 * @return the rowIndex
	 */
	public int getRowIndex() {
		return rowIndex;
	}

	/**
	 * @param rowIndex
	 *            the rowIndex to set
	 */
	public void setRowIndex(int rowIndex) {
		this.rowIndex = rowIndex;
	}

	/**
	 * @return the columnIndex
	 */
	public int getColumnIndex() {
		return columnIndex;
	}

	/**
	 * @param columnIndex
	 *            the columnIndex to set
	 */
	public void setColumnIndex(int columnIndex) {
		this.columnIndex = columnIndex;
	}

	/**
	 * @return the object
	 */
	public Object getObject() {
		return object;
	}

	/**
	 * @param object
	 *            the object to set
	 */
	public void setObject(Object object) {
		this.object = object;
	}

	/**
	 * @return the rowValidationParam
	 */
	public RowValidationParam getRowValidationParam() {
		return rowValidationParam;
	}

	/**
	 * @param rowValidationParam
	 *            the rowValidationParam to set
	 */
	public void setRowValidationParam(RowValidationParam rowValidationParam) {
		this.rowValidationParam = rowValidationParam;
	}

	/**
	 * @return the value
	 */
	public Object getValue() {
		return value;
	}

	/**
	 * @param value
	 *            the value to set
	 */
	public void setValue(Object value) {
		this.value = value;
	}

	/**
	 * @return the validationLabel
	 */
	public String getValidationLabel() {
		return validationLabel;
	}

	/**
	 * @param validationLabel
	 *            the validationLabel to set
	 */
	public void setValidationLabel(String validationLabel) {
		this.validationLabel = validationLabel;
	}

	/**
	 * @return the attributeTemplate
	 */
	public AttributeTemplate getAttributeTemplate() {
		return attributeTemplate;
	}

	/**
	 * @param attributeTemplate
	 *            the attributeTemplate to set
	 */
	public void setAttributeTemplate(AttributeTemplate attributeTemplate) {
		this.attributeTemplate = attributeTemplate;
	}

	/**
	 * @return the dataGridSource
	 */
	public DataGridSource<?> getDataGridSource() {
		return dataGridSource;
	}

	/**
	 * @param dataGridSource
	 *            the dataGridSource to set
	 */
	public void setDataGridSource(DataGridSource<?> dataGridSource) {
		this.dataGridSource = dataGridSource;
	}

}
